﻿<?LassoScript

	/*

		lp8-5-5/test_b_template.lasso

		This is a test-suite for running Douglas Burchard's B-Suite of custom types 
		against an L-Unit unit-test environment. To run this test-suite, you'll need 
		to first install L-Unit™ 2.0, then custom configure the file paths within 
		this file.

		Please see l-unit.org for more information. Do not install this test-suite on 
		a live production server.

		The MIT License

		Copyright (c) 2009, Douglas Burchard.
		
		Permission is hereby granted, free of charge, to any person obtaining a copy
		of this software and associated documentation files (the "Software"), to deal
		in the Software without restriction, including without limitation the rights
		to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
		copies of the Software, and to permit persons to whom the Software is
		furnished to do so, subject to the following conditions:

		The above copyright notice and this permission notice shall be included in
		all copies or substantial portions of the Software.

		THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
		IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
		FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
		AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
		LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
		OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
		THE SOFTWARE.

	*/

	Define_Type('Test_B_Template', 'Unit_TestCase');

		Define_Tag('Setup');
			Library('/appClasses/b/b.lasso');
			Var('moof' = B_Template);
		/Define_Tag;

		Define_Tag('Teardown');
			Vars -> Remove('moof');
		/Define_Tag;

		Define_Tag('CustomNote', -Required='package');
			Self -> StartTask;

			(Self -> 'taskPassed') = true;
			(Self -> 'taskMessage') = String(Local('package'));

			Self -> StoreResult(
				Map(
					'name'='Custom Note',
					'result'=(Self -> 'taskPassed'),
					'message'=(Self -> 'taskMessage'),
					'time'=(Self -> EndTask)
					)
				);
		/Define_Tag;

		Define_Tag('CustomAssert_StringEquals', -Required='package', -Required='equals');
			Self -> StartTask;

			If((Local('package') -> Type) != (String -> Type));
				(Self -> 'taskPassed') = false;
				(Self -> 'taskMessage') = String('failed: ' + Local('package') + ' -> Type == ' + (Local('package') -> Type) + ', which is not String.');
			Else(Local('package') == Local('equals'));
				(Self -> 'taskPassed') = true;
				(Self -> 'taskMessage') = String('passed');
			Else;
				(Self -> 'taskPassed') = false;
				(Self -> 'taskMessage') = String('failed: "' + Local('package') + '" != "' + Local('equals') + '"');
			/If;

			Self -> StoreResult(
				Map(
					'name'='CustomAssert_StringEquals',
					'result'=(Self -> 'taskPassed'),
					'message'=(Self -> 'taskMessage'),
					'time'=(Self -> EndTask)
					)
				);
		/Define_Tag;

		Define_Tag('Test_Location');
			Self -> CustomNote('The default-value of (#tempalte -> Location) should be of type String, with a size of 0 (zero).');
			Self -> Assert_IsString($moof -> Location);
			Self -> Assert_IsSizeOf(($moof -> Location), -Size=(0));

			Self -> CustomNote('After setting the (#template -> Location) value to \'///Library/Tenon/WebServer/WebSites/unit/appClasses/b/template.lasso\', the type should still be String, the size should be 68, and the value should be correct.');
			$moof -> Location('///Library/Tenon/WebServer/WebSites/unit/appClasses/b/template.lasso');
			Self -> Assert_IsString($moof -> Location);
			Self -> Assert_IsSizeOf(($moof -> Location), -Size=(68));
			Self -> CustomAssert_StringEquals(($moof -> Location), -Equals='///Library/Tenon/WebServer/WebSites/unit/appClasses/b/template.lasso');
		/Define_Tag;

		Define_Tag('Test_MimeType');
			Self -> CustomNote('The default-value of (#tempalte -> MimeType) should be of type String, with a size of 9 (nine).');
			Self -> Assert_IsString($moof -> MimeType);
			Self -> Assert_IsSizeOf(($moof -> MimeType), -Size=(9));

			Self -> CustomNote('After setting the (#template -> MimeType) value to \'text/xml\', the type should still be String, the size should be 8, and the value should be correct.');
			$moof -> MimeType('text/xml');
			Self -> Assert_IsString($moof -> MimeType);
			Self -> Assert_IsSizeOf(($moof -> MimeType), -Size=(8));
			Self -> CustomAssert_StringEquals(($moof -> MimeType), -Equals='text/xml');
		/Define_Tag;

		Define_Tag('Test_Base');
			Self -> CustomNote('The default-value of (#tempalte -> Base) should be of type String, with a size of 0 (zero).');
			Self -> Assert_IsString($moof -> Base);
			Self -> Assert_IsSizeOf(($moof -> Base), -Size=(0));
			Self -> CustomNote('After setting the (#template -> Location) value to \'///Library/Tenon/WebServer/WebSites/unit/appClasses/b/template.lasso\', the return-type of (#template -> Base) should still be String, the size should be 219, and the value should be correct.');
			$moof -> Location('///Library/Tenon/WebServer/WebSites/unit/appClasses/b/template.lasso');
			Self -> Assert_IsString($moof -> Base);
			Self -> Assert_IsSizeOf(($moof -> Base), -Size=(291));
		/Define_Tag;

		Define_Tag('Test_CurrentInstance');
			Self -> CustomNote('The default-value of (#tempalte -> CurrentInstance) should be of type String, with a size of 0 (zero).');
			Self -> Assert_IsString($moof -> CurrentInstance);
			Self -> Assert_IsSizeOf(($moof -> CurrentInstance), -Size=(0));
			Self -> CustomNote('After setting the (#template -> Location) value to \'///Library/Tenon/WebServer/WebSites/unit/appClasses/b/template.lasso\', the return-type of (#template -> CurrentInstance) should still be String, the size should be 219, and the value should be correct.');
			$moof -> Location('///Library/Tenon/WebServer/WebSites/unit/appClasses/b/template.lasso');
			Self -> Assert_IsString($moof -> CurrentInstance);
			Self -> Assert_IsSizeOf(($moof -> CurrentInstance), -Size=(291));
		/Define_Tag;

		Define_Tag('Test_Apply');
			Self -> CustomNote('First, we\'ll load our test-template, and check to make certain the content loaded correctly.');
			$moof -> Location('///Library/Tenon/WebServer/WebSites/unit/appClasses/b/template.lasso');
			Self -> Assert_IsStringContaining(($moof -> CurrentInstance), -Contains='<!-- $My Test -->');
			Self -> Assert_IsSizeOf(($moof -> CurrentInstance), -Size=(291));

			Self -> CustomNote('Next, using the Apply member-tag, we\'ll alter the placeholder from "&lt;!-- $My Test --&gt;" to "&lt;p&gt;My replacement string.&lt;/p&gt;", and check to make certain the change has been made.');
			$moof -> Apply('<!-- $My Test -->'='<p>My replacement string.</p>');
			Self -> Assert_IsStringContaining(($moof -> CurrentInstance), -Contains='<p>My replacement string.</p>');
			Self -> Assert_IsSizeOf(($moof -> CurrentInstance), -Size=(303));

			Self -> CustomNote('Finally, using regular expressions, we\'ll alter the title from "My Test" to "My Title", and check to make certain this change has been made as well.');
			$moof -> Apply(RegExp(-Find='(?<=<title>)([mM][yY]\\s?).*(?=</title>)', -Replace='$1Title'));
			Self -> Assert_IsStringContaining(($moof -> CurrentInstance), -Contains='<title>My Title</title>');
			Self -> Assert_IsSizeOf(($moof -> CurrentInstance), -Size=(304));
		/Define_Tag;

		Define_Tag('Test_ApplyToBase');
			Self -> CustomNote('First, we\'ll load our test-template, and using the Apply member-tag, alter the title from "My Test" to "My Title", and finally check to make certain the change has been made.');
			$moof -> Location('///Library/Tenon/WebServer/WebSites/unit/appClasses/b/template.lasso');
			$moof -> Apply('<title>My Test</title>'='<title>My Title</title>');
			Self -> Assert_IsStringContaining(($moof -> CurrentInstance), -Contains='<title>My Title</title>');
			Self -> Assert_IsSizeOf(($moof -> CurrentInstance), -Size=(292));

			Self -> CustomNote('Next, using the ApplyToBase member-tag, we\'ll make a similar change from the original "My Test" to "My New Title", and test to see if this change has been made.');
			$moof -> ApplyToBase('<title>My Test</title>'='<title>My New Title</title>');
			Self -> Assert_IsStringContaining(($moof -> CurrentInstance), -Contains='<title>My New Title</title>');
			Self -> Assert_IsSizeOf(($moof -> CurrentInstance), -Size=(296));

			Self -> CustomNote('Finally, using regular expressions, we\'ll alter the title from "My Test" to "My Title", and check to make certain this change has been made as well.');
			$moof -> ApplyToBase(RegExp(-Find='(?<=<title>)([mM][yY]\\s?).*(?=</title>)', -Replace='$1Title'));
			Self -> Assert_IsStringContaining(($moof -> CurrentInstance), -Contains='<title>My Title</title>');
			Self -> Assert_IsSizeOf(($moof -> CurrentInstance), -Size=(292));
		/Define_Tag;

		Define_Tag('Test_Extract');
			Library('/appClasses/b/b.lasso');
			Self -> CustomNote('First, we\'ll load our test-template, and check to make certain the content loaded correctly.');
			$moof -> Location('///Library/Tenon/WebServer/WebSites/unit/appClasses/b/template.lasso');
			Self -> Assert_IsStringContaining(($moof -> CurrentInstance), -Contains='<!-- $My Test -->');
			Self -> Assert_IsSizeOf(($moof -> CurrentInstance), -Size=(291));

			Self -> CustomNote('Second, we\'ll extract a portion of the loaded test-template, and test to make sure the extracted contents are correct.');
			Local('mysubtemplate' = ($moof -> Extract('(?<=<ul>)\\s*<li>Article Title<\/li>(?=\\s*<\/ul>)')));
			Self -> Assert_IsStringContaining((#mysubtemplate -> CurrentInstance), -Contains='Article Title');

			Local('myinsertion' = String);
			Self -> CustomNote('Third, we\'ll iterate over an array of strings, using each string as a replacement for a portion of our extracted template. The results of each will be appended onto a variable created for this purpose.');
			Iterate(Array('foo', 'bar'), Local('str'));
				#mysubtemplate -> ApplyToBase('Article Title'=#str);
				#myinsertion += (#mysubtemplate -> CurrentInstance);
			/Iterate;
			$moof -> Apply((#mysubtemplate -> Base)=#myinsertion);
			Self -> Assert_IsStringContaining(($moof -> CurrentInstance), -Contains='<li>foo</li>');
			Self -> Assert_IsStringContaining(($moof -> CurrentInstance), -Contains='<li>bar</li>');
			Self -> Assert_IsSizeOf(($moof -> CurrentInstance), -Size=(297));

			Self -> CustomNote('Finally, we\'ll extract a portion of the loaded test-template based on a regular expression, and test to make sure the extracted contents are correct.');
			Local('mysubtemplate' = ($moof -> Extract('(?s)<!-- Companies -->.*<!-- /Companies -->')));
			Self -> Assert_IsStringContaining((#mysubtemplate -> CurrentInstance), -Contains='Company');
			Self -> Assert_IsSizeOf((#mysubtemplate -> CurrentInstance), -Size=(69));
		/Define_Tag;

	/Define_Type;

?>